package com.obrempon.credit.market.ui.adapter

import android.annotation.SuppressLint
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.text.style.UnderlineSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView
import com.obrempon.credit.market.R
import com.obrempon.credit.market.databinding.ObremponApplyDetailsChildItemBinding
import com.obrempon.credit.market.utils.ObremponDateUtil
import com.obrempon.module.adapter.ObremponRecyclerViewAdapter
import com.obrempon.module.adapter.ObremponViewHolderHelper
import org.json.JSONObject
import java.math.BigDecimal

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/11/19 10:43
 * @Annotation：订单列表适配器
 */
class ObremponRepayListApt(recyclerView: RecyclerView?) : ObremponRecyclerViewAdapter<JSONObject>(recyclerView) {

    override fun getItemViewType(position: Int): Int {
        val model = getItem(position)
        val itemType = model.optString("itemType")
        return when(itemType){
            "emptyItemView" -> {
                R.layout.obrempon_order_list_empty
            }
            else -> {
                R.layout.obrempon_repay_list_item
            }
        }
    }

    override fun setItemChildListener(helper: ObremponViewHolderHelper, viewType: Int) {
        helper.setItemChildClickListener(R.id.buttonRepay)
        helper.setItemChildClickListener(R.id.textEmpty)
    }

    override fun fillData(helper: ObremponViewHolderHelper, position: Int, model: JSONObject) {
        val itemType = model.optString("itemType")
        when (itemType){
            "emptyItemView" -> {//空
                val showEmptyText = String.format(getString(R.string.obrempon_repay_empty),getString(R.string.obrempon_home_applied_empty_apply_now))
                val startIndex = showEmptyText.indexOf(getString(R.string.obrempon_home_applied_empty_apply_now))
                val emptySpannable = SpannableString(showEmptyText)
                emptySpannable.setSpan(ForegroundColorSpan(getColor(R.color.color_ffff7944)), startIndex, showEmptyText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                //添加下划线
                emptySpannable.setSpan(UnderlineSpan(), startIndex, showEmptyText.length, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
                val textEmpty = helper.getTextView(R.id.textEmpty)
                textEmpty.text = emptySpannable

//                val textEmpty = helper.getTextView(R.id.textEmpty)
//                textEmpty.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
//                textWithdraw.paint.isAntiAlias = true//抗锯齿
            }
            else -> {
                showObremponRepayItemView(helper,position,model)
            }
        }
    }

    /**
     * 订单itemView
     */
    @SuppressLint("SetTextI18n")
    private fun showObremponRepayItemView(helper: ObremponViewHolderHelper, position: Int, model: JSONObject){
        //单位
        helper.setText(R.id.textRepayTotal,"${model.optString("repayAmount")} ${model.optString("currency")}")
        helper.setText(R.id.textRepayRemainingDays,String.format(getString(R.string.obrempon_order_details_loan_term_days),model.optString("remainRepayDay")))

        val detailsItemLayout = helper.getView<LinearLayout>(R.id.detailsItemLayout)
        detailsItemLayout.removeAllViews()
        if (model.has("repayInfo")){
            val repayInfo = model.optJSONObject("repayInfo")
            if (repayInfo != null){
                helper.setText(R.id.textRepayDueDate,repayInfo.optString("repayDate"))

                val detailItemList = repayInfo.optJSONArray("detailItemList")
                if (detailItemList != null){
                    for (i in 0..< detailItemList.length()){
                        val item = detailItemList.optJSONObject(i)
                        val childView = ObremponApplyDetailsChildItemBinding.inflate(LayoutInflater.from(mContext))
                        childView.textDetailsTitle.text = item.optString("showName")
                        childView.textDetailsTitle.setTextColor(getColor(R.color.color_ff999999))
                        childView.textDetailsValue.text = item.optString("showDetail")
                        childView.textDetailsValue.setTextColor(getColor(R.color.color_ff666666))

                        childView.textOldValue.visibility = View.GONE
                        if (item.has("oldDetail")){
                            childView.textOldValue.visibility = View.VISIBLE
                            childView.textOldValue.text = item.optString("oldDetail")
                            childView.textDetailsValue.setTextColor(getColor(R.color.color_ffff3e30))
                        }
                        childView.discountLayout.visibility = View.GONE
                        if (item.has("discount")){
                            childView.discountLayout.visibility = View.VISIBLE
                            childView.textDiscount.text = item.optString("discount")
                        }
                        detailsItemLayout.addView(childView.root)
                    }
                }
            }
        }
    }
}